//
//  WQCell.m
//  货运代码
//
//  Created by 刘胤辰 on 16/5/5.
//  Copyright © 2016年 温泉. All rights reserved.
//

#import "FDAttentionCell.h"
#import "FDMDataModel.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "UIView+Extension.h"
#import "UILabel+FDExtension.h"
#import "UIButton+FDExtension.h"

@interface FDAttentionCell()

//医生头像
@property (nonatomic , weak)UIImageView *doctorImageView;
//医生名字
@property (nonatomic , weak)UILabel *nameLabel;
//职称
@property (nonatomic , weak)UILabel *titleLabel;
//医院
@property (nonatomic , weak)UILabel *hospitalLabel;
//关注度
@property (nonatomic , weak)UIButton *accuracyButton;
//三个小按钮
@property (nonatomic , weak)UIButton *plusButton;
@property (nonatomic , weak)UIButton *flowerButton;
@property (nonatomic , weak)UIButton *starButton;

@end

@implementation FDAttentionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
        [self setupUI];
    }
    return self;
}

/**修改cell的frame*/
- (void)setFrame:(CGRect)frame
{
    CGFloat margin = SCREEN_WIDTH*0.04;
    //每个cell向下偏移20(整体都向下偏移了20)
    frame.origin.y += margin;
    //每个cell高度减少20(每个cell上下缩进相同距离)
    frame.size.height -=margin;
    [super setFrame:frame];
}

/**创建子控件*/
- (void)setupUI
{
    //医生头像
    UIImageView *doctorImageView = [[UIImageView alloc]init];
    self.doctorImageView = doctorImageView;
    [self.contentView addSubview:doctorImageView];
    
    //医生名字
    UILabel *nameLabel = [UILabel setCustomLabel];
    self.nameLabel = nameLabel;
    [self.contentView addSubview:nameLabel];
    
    //职称
    UILabel *titleLabel = [UILabel setLightGrayColorLabelWithString:nil];
    self.titleLabel = titleLabel;
    [self.contentView addSubview:titleLabel];
    
    //医院
    UILabel *hospitalLabel = [UILabel setLightGrayColorLabelWithString:nil];
    self.hospitalLabel = hospitalLabel;
    [self.contentView addSubview:hospitalLabel];
    
    //关注度
    UIButton *accuracyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.accuracyButton = accuracyButton;
    [accuracyButton setBackgroundImage:[UIImage imageNamed:@"pipeidu"] forState:UIControlStateNormal];
    accuracyButton.titleLabel.font = [UIFont systemFontOfSize:13];
    accuracyButton.titleLabel.numberOfLines = 2;
    accuracyButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:accuracyButton];
    
    //三个小按钮
    UIButton *plusButton = [UIButton setCustomButtonWithImage:@"yuyueliang" andTitle:@"  111"];
    self.plusButton = plusButton;
    [self.contentView addSubview:plusButton];
    
    UIButton *flowerButton = [UIButton setCustomButtonWithImage:@"xianhua" andTitle:@"  222"];
    self.flowerButton = flowerButton;
    [self.contentView addSubview:flowerButton];
    
    UIButton *starButton = [UIButton setCustomButtonWithImage:@"jinqi" andTitle:@"  333"];
    self.starButton = starButton;
    [self.contentView addSubview:starButton];
}

/**布局子控件*/
-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    //医生头像
    [self.doctorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(10);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    
    //医生名字
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.doctorImageView.mas_right).offset(10);
        make.top.mas_equalTo(self.doctorImageView.mas_top);
    }];
    
    //职称
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_top);
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(10);
    }];
    
    //医院
    [self.hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.doctorImageView.mas_right).offset(10);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10);
    }];
    
    //匹配度
    [self.accuracyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.right.offset(-30);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH*0.15, SCREEN_WIDTH*0.2));
    }];
    
    //三个小按钮约束
    [self.plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.hospitalLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(self.nameLabel.mas_left);
    }];
    
    [self.flowerButton  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.plusButton.mas_top);
        make.left.mas_equalTo(self.plusButton.mas_right).offset(10);
    }];
    
    [self.starButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.flowerButton.mas_top);
        make.left.mas_equalTo(self.flowerButton.mas_right).offset(10);
    }];
}

/**为控制赋值*/
- (void)setDataModel:(FDMDataModel *)dataModel
{
    _dataModel = dataModel;
    
    //医生头像
    //1.url地址
    NSURL *url = [NSURL URLWithString:dataModel.doctor_portrait];
    //2.占位图
    UIImage *placeholderImage = [[UIImage imageNamed:@"illness_img_person.png"] circleImage];
    //3.imageView设置网络图片
    [self.doctorImageView sd_setImageWithURL:url placeholderImage:placeholderImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        //如果URL地址正确才替换为网络图片,地址错误依然使用占位图片
        if (image) {
            self.doctorImageView.image = [image circleImage];
        }
        
    }];
    
    //医生名字
    self.nameLabel.text = dataModel.doctor_name;
    
    //职称
    self.titleLabel.text = dataModel.doctor_title_name;
    
    //医院
    self.hospitalLabel.text = dataModel.doctor_hospital_name;
    
    //关注度
    [self.accuracyButton setTitle:[NSString stringWithFormat:@"匹配度%@",dataModel.accuracy] forState:UIControlStateNormal];
    
}


@end
